/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the MatrixParser package.
 *  
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.matrixparser.features;

import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.Map.Entry;
import org.osdtsystem.matrixparser.features.keys.FeatureKey;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public abstract class AbstractFeatureVector extends AbstractCollection<Entry<Feature,Double>>
        implements FeatureVector, WeightVector, Serializable {
    public void addValue(FeatureHandler fh, FeatureKey key, double v) {
        addValue(fh.getFeature(key), v);
    }

    public void addTo(float scalar, FeatureVector fv) {
        for (Entry<Feature,Double> entry : this) {
            double value = entry.getValue();
            if (value != 0) {
                Feature key = entry.getKey();
                fv.setValue(key, scalar * value + fv.getValue(key));
            }
        }
    }

    public double innerProduct(FeatureVector fv) {
        // Let smallest feature vector compute the inner product
        if (this.size() > fv.size()) {
            return fv.innerProduct(this);
        }

        // Compute the inner product
        double result = 0;
        for (Entry<Feature,Double> entry : this) {
            result += entry.getValue() * fv.getValue(entry.getKey());
        }

        // Return
        return result;
    }

    public void trimToSize() {
    }
}
